package com.crm.system.service.impl.permissionManagement;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysCompany;
import com.crm.model.qo.sys.CompanyQO;
import com.crm.model.vo.sys.CompanyVO;
import com.crm.model.vo.sys.IssueInvoiceVO;
import com.crm.service.sys.permissionManagement.SysCompanyDbService;
import com.crm.system.service.permissionManagement.SysCompanyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ Author ：pym
 * @ Date   ：2020/12/9 9:00
 */
@Service
public class SysCompanyServiceImpl implements SysCompanyService {
    @Autowired
    private SysCompanyDbService sysCompanyDbService;

    @Override
    public List<SysCompany> selectSysCompanyList() {
        List<SysCompany> sysCompanyList = sysCompanyDbService.selectSysCompanyList();
        return sysCompanyList;
    }

    @Override
    public SysCompany selectSysCompanyById(String companyId) {
        SysCompany sysCompany = sysCompanyDbService.selectSysCompanyById(companyId);
        return sysCompany;
    }

    /**
     * 查询所有未删除的公司(管理模块--线程表单所属公司下拉搜索框)
     * @return
     */
    public List<Map<String, Object>> selectSysCompanySelectList(){
        return sysCompanyDbService.selectSysCompanyList((short)StatusCodeEnum.NORMAL_CODE.getState());
    }

    /**
     * 查询所有公司+税盘号
     * @return
     */
    @Override
    public List<Map<String, Object>> selectCompanyAndTaxNumberList() {
        return sysCompanyDbService.selectCompanyAndTaxNumberList();
    }

    /**
     * 根据公司查询公司税号
     * @param companyId
     * @return
     */
    @Override
    public String selectCompanyTaxNumber(String companyId) {
        return sysCompanyDbService.selectCompanyTaxNumber(companyId);
    }

    /***
     * @Description:查询公司列表
     * @Param: pageIndex/pageSize 页码/每页显示个数
     * @Author: luojie
     * @Date: 2021/5/19 16:14
     */
    @Override
    public List<CompanyVO> selectCompanyList(Integer pageIndex, Integer pageSize,String companyName) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        List<CompanyVO> companyVOList = new ArrayList<>();
        List<SysCompany> sysCompanyList = sysCompanyDbService.selectSysCompanyList(companyName);
        sysCompanyList.stream().forEach(sysCompany ->{
            CompanyVO companyVO = new CompanyVO();
            BeanUtils.copyProperties(sysCompany,companyVO);
            companyVOList.add(companyVO);
        });
        return companyVOList;
    }

    @Override
    public Boolean addCompany(CompanyQO companyQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        SysCompany sysCompany = new SysCompany();
        sysCompany.setCompanyId(CommonUtils.getGUID());
        sysCompany.setCompanyCode(companyQO.getCompanyCode());
        sysCompany.setCompanyName(companyQO.getCompanyName());
        sysCompany.setCityCode(companyQO.getCityCode());
        sysCompany.setCityName(companyQO.getCityName());
        sysCompany.setProvinceCode(companyQO.getProvinceCode());
        sysCompany.setProvinceName(companyQO.getProvinceName());
        sysCompany.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        sysCompany.setCreateTm(new Date());
        sysCompany.setUpdateTm(new Date());
        sysCompany.setCreatePeopleId(userAccount.getAccountId());
        sysCompany.setUpdatePeopleId(userAccount.getAccountId());
        sysCompany.setSysId("2");
        Boolean result = sysCompanyDbService.insertCompany(sysCompany);

        return result;
    }

    @Override
    public SysCompany selectSysCompanyByCompanyName(String companyName) {
        SysCompany sysCompany = sysCompanyDbService.selectSysCompanyByName(companyName);
        return sysCompany;
    }

}
